package main

import (
	"bytes"
	"fmt"
	"os/exec"
	"strings"
	"time"
)

func main() {
	var branch = getCurrentBranch()
	fmt.Println("current-branch:", branch)
	for !push(branch) {
	}
}

func push(branch string) bool {
	fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "git push origin", branch)
	cmd := exec.Command("git", "push", "origin", branch)
	var out bytes.Buffer
	cmd.Stdout = &out
	cmd.Stderr = &out
	cmd.Run()
	result := out.String()
	result = strings.Trim(result, "\r")
	result = strings.Trim(result, "\n")
	result = strings.TrimSpace(result)
	fmt.Println(result)
	if result == "Everything up-to-date" {
		return true
	}
	return false
}

func getCurrentBranch() string {
	cmd := exec.Command("git", "branch", "--show-current")
	var out bytes.Buffer
	cmd.Stdout = &out
	cmd.Stderr = &out
	cmd.Run()
	branch := out.String()
	branch = strings.Trim(branch, "\r")
	branch = strings.Trim(branch, "\n")
	return branch
}
